/**
 *
 */
package com.qwiki.xml;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.qwiki.page.WikiText;
import com.qwiki.text.TextNode;

/**
 * WikiXMLPage
 *
 * @author xgu
 * @since Jun 26, 2012
 */
public class WikiXMLPage
{
  int id;
  int namespace;

  public int getId()
  {
    return id;
  }

  public void setId(int id)
  {
    this.id = id;
  }

  public int getNamespace()
  {
    return namespace;
  }

  public void setNamespace(int namespace)
  {
    this.namespace = namespace;
  }

  public String getTitle()
  {
    return title;
  }

  public void setTitle(String title)
  {
    this.title = title;
  }

  public String getText()
  {
    return text;
  }

  public void setText(String text)
  {
    this.text = text;
  }

  public String getRedirect()
  {
    return redirect;
  }

  public boolean isRedirect()
  {
    return redirect != null;
  }

  String title;
  String text;
  String redirect;
  private WikiPage wikiPage;

  public void put(String name, String value)
  {
    if ("ns".equals(name))
    {
      namespace = Integer.parseInt(value);
    } else if ("id".equals(name))
    {
      id = Integer.parseInt(value);
    } else if ("title".equals(name))
    {
      title = value;
    } else if ("text".equals(name))
    {
      text = value;
    } else if ("redirect".equals(name))
    {
      redirect = "".equals(value) ? null : value;
    }
  }

  public static String parseWiki(String text, String title)
  {
    TextNode node = new TextNode();
    char[] buffer = text.toCharArray();
    node.parse(buffer, 0);
    if (node.end != buffer.length)
    {
      System.out.println(title);
      System.out.println("---------------unfinished parse");
      System.out.println(text);
      System.exit(0);
    }
    String plaintext = node.getText();
    plaintext = plaintext.replaceAll("<sub>", " ");
    plaintext = plaintext.replaceAll("</sub>", " ");
    plaintext = plaintext.replaceAll("<sup>", " ");
    plaintext = plaintext.replaceAll("</sup>", " ");
    plaintext = plaintext.replaceAll("<center>", " ");
    plaintext = plaintext.replaceAll("</center>", " ");
    plaintext = plaintext.replaceAll("<div>", " ");
    plaintext = plaintext.replaceAll("</div>", " ");
    plaintext = plaintext.replaceAll("&nbsp;", " ");
    plaintext = plaintext.replaceAll("&mdash;", "—");
    plaintext = plaintext.replaceAll("&ndash;", "–");
    plaintext = plaintext.replaceAll("<blockquote>|</blockquote>", " ");
    return plaintext;
  }

  public static WikiText parseWikitext(String text, String title)
  {
    TextNode node = new TextNode();
    char[] buffer = text.toCharArray();
    node.parse(buffer, 0);
    if (node.end != buffer.length)
    {
      System.out.println(title);
      System.out.println("---------------unfinished parse");
      System.out.println(text);
      System.exit(0);
    }
    String plaintext = node.getText();
    plaintext = plaintext.replaceAll("<sub>", " ");
    plaintext = plaintext.replaceAll("</sub>", " ");
    plaintext = plaintext.replaceAll("<sup>", " ");
    plaintext = plaintext.replaceAll("</sup>", " ");
    plaintext = plaintext.replaceAll("<center>", " ");
    plaintext = plaintext.replaceAll("</center>", " ");
    plaintext = plaintext.replaceAll("<div>", " ");
    plaintext = plaintext.replaceAll("</div>", " ");
    plaintext = plaintext.replaceAll("&nbsp;", " ");
    plaintext = plaintext.replaceAll("&mdash;", "—");
    plaintext = plaintext.replaceAll("&ndash;", "–");
    plaintext = plaintext.replaceAll("<blockquote>|</blockquote>", " ");
    WikiText wikitext= new WikiText();
    wikitext.title = title;
    wikitext.text = plaintext;
    wikitext.categories = node.getCategories();
    wikitext.entities = node.getEntities();
    wikitext.more = node.getCategoryMore(title);
    return wikitext;
  }

  public WikiPage parse()
  {
    if (wikiPage == null)
    {
      WikiText page = parseWikitext(text, title);
      wikiPage = new WikiPage();
      wikiPage.setId(id);
      wikiPage.setTitle(title.trim().replaceAll(" ", "_"));
      wikiPage.setNamespace(namespace);
      wikiPage.setText(filter(page.text.trim()));
      String[] myCategories = new String[page.categories.length];
      for(int i = 0; i< myCategories.length; i++){
        myCategories[i] = page.categories[i].trim().replaceAll(" ", "_");
      }
      wikiPage.setCategories(myCategories);
      wikiPage.setEntities(page.entities);
    }
    return wikiPage;
  }

  private static String reg1 = "<big>|</big>|<small>|</small>|<i>|</i>|<sub>|</sub>|<sup>|</sup>|<center>|</center>"
      + "|<div>|</div>|<blockquote>|</blockquote>|&nbsp;";
  private static Pattern pattern1 = Pattern.compile(reg1);

  private String filter(String text)
  {
    Matcher matcher = pattern1.matcher(text);
    text = matcher.replaceAll(" ");
    text = text.replaceAll("&mdash;", "—");
    text = text.replaceAll("&ndash;", "–");
    return text;
  }

  @Override
  public String toString()
  {
    return "id: " + id + " - " + title + " - ns: " + namespace + " - " + text;
  }
}
